----------------------------------------------------------------------------------
-- Company: 
-- Engineer: Siebe Krijgsman
-- 
-- Create Date:    17:11:10 12/04/2008 
-- Design Name: 
-- Module Name:    VGA_contoller - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 1.10 - working vga output, bars and chars working
--	Revision 1.20a - changed resolution to 800x600@60Hz (pixel_clk = 40 MHz)
--	Revision 1.21a - changed to dual fft input (before/after)
--	Revision 1.30a - added control feedback
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use WORK.VGA_defines.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity VGA_controller is
    Port ( 	clk_40M : in  STD_LOGIC;
				clk_48k: in std_logic;
				reset : in  STD_LOGIC;
			  
				time_control_out			: in std_logic_vector(3 downto 0);
				freq_control_out			: in std_logic_vector(3 downto 0);
				main_control_in			: in std_logic_vector(7 downto 0);
				time_control_in			: in std_logic_vector(7 downto 0);
				freq_control_in			: in std_logic_vector(7 downto 0);
				keyboard_control_data	: in std_logic_vector(7 downto 0);
				keyboard_data_valid		: in std_logic;
				
				fft_data_re_in_left_0	: in std_logic_vector(23 downto 0);
				fft_data_im_in_left_0	: in std_logic_vector(23 downto 0);
				fft_data_re_in_left_1	: in std_logic_vector(23 downto 0);
				fft_data_im_in_left_1	: in std_logic_vector(23 downto 0);
				fft_data_re_in_left_2	: in std_logic_vector(23 downto 0);
				fft_data_im_in_left_2	: in std_logic_vector(23 downto 0);
				sample_number	: in std_logic_vector(6 downto 0);
			
				VGA_OUT_RED : out  STD_LOGIC_VECTOR (7 downto 0);
				VGA_OUT_GREEN : out  STD_LOGIC_VECTOR (7 downto 0);
				VGA_OUT_BLUE : out  STD_LOGIC_VECTOR (7 downto 0);
				VGA_OUT_PIXEL_CLOCK : out  STD_LOGIC;
				VGA_COMP_SYNCH : out  STD_LOGIC;
				VGA_OUT_BLANK_Z : out  STD_LOGIC;
				VGA_HSYNCH : out  STD_LOGIC;
				VGA_VSYNCH : out  STD_LOGIC);
end VGA_controller;

architecture Behavioral of VGA_controller is
	
	component character_rom IS
	port (
		addr: IN std_logic_VECTOR(9 downto 0);
		clk: IN std_logic;
		dout: OUT std_logic_VECTOR(8 downto 0));
	END component character_rom;
	
	-- internals
	signal pixel_clk_i	: std_logic := '0';
	
	signal comp_synch_i	: std_logic := '0';
   signal blank_i			: std_logic := '0';
   signal hsynch_i		: std_logic := '0';
   signal vsynch_i		: std_logic := '0';
	
	-- signals
	signal pixel_counter	: std_logic_vector(11 downto 0) := (others => '0');
	signal line_counter	: std_logic_vector(10 downto 0) := (others => '0');
	signal h_blank			: std_logic := '0';
	signal v_blank			: std_logic := '0';
	signal h_synch			: std_logic := '0';
	signal v_synch			: std_logic := '0';
	signal pixel_clk_dcm	: std_logic := '0';
	
	signal reset_n			: std_logic := '1';
	signal char_matrix		: std_logic_vector(8 downto 0) := (others => '0');
	signal char_h_count		: integer := 0;
	signal char_v_count		: std_logic_vector(3 downto 0) := (others => '0');
	signal char_h_position	: integer := 0;
	signal char_v_position	: integer := 0;
	
	--sample 0 (before)
	signal temp_data_0				: fft_data_struct := (others => (others => '0'));
	signal graph_data_0				: fft_data_struct := (others => (others => '0'));
	signal end_of_video_frame_0	: std_logic := '0';
	signal start_new_audio_frame_0: std_logic := '0';
	signal audio_frame_ready_0		: std_logic := '0';
	signal begin_fetch_0				: std_logic := '0';
	signal last_count_0				: std_logic_vector(6 downto 0) := (others => '0');
	
	--sample 1(after)
	signal temp_data_1				: fft_data_struct := (others => (others => '0'));
	signal graph_data_1				: fft_data_struct := (others => (others => '0'));
	signal end_of_video_frame_1	: std_logic := '0';
	signal start_new_audio_frame_1: std_logic := '0';
	signal audio_frame_ready_1		: std_logic := '0';
	signal begin_fetch_1				: std_logic := '0';
	signal last_count_1				: std_logic_vector(6 downto 0) := (others => '0');

	signal digit_1					: std_logic_vector(7 downto 0) := (others => '0');
	signal digit_2					: std_logic_vector(7 downto 0) := (others => '0');
	signal clk_counter			: integer	:= 0;
	signal start					: std_logic	:= '0';
	
	signal key_data				: std_logic_vector(7 downto 0) := (others => '0');
	
	signal rom_address			: std_logic_vector(9 downto 0) := (others => '0');
	signal rom_data				: std_logic_vector(8 downto 0) := (others => '0');
	signal data_request			: std_logic := '0';
begin
	
	char_rom : character_rom
		port map (
			addr => rom_address,
			clk => clk_40M,
			dout => rom_data);
	
	VGA_OUT_PIXEL_CLOCK	<= clk_40M;

	pixel_clk_i				<= clk_40M;
	
	--DCM uses active high reset
	reset_n <= not reset;
	
   VGA_COMP_SYNCH			<= comp_synch_i;
   VGA_OUT_BLANK_Z		<= not blank_i;
   VGA_HSYNCH				<= hsynch_i;
   VGA_VSYNCH				<= vsynch_i;

	h_counter: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			pixel_counter <= (others => '0');
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if pixel_counter = (H_TOTAL - 1) then													--last pixel of the current line
				pixel_counter <= (others => '0');
			else
				pixel_counter <= pixel_counter + 1;
			end if;
		end if;
	end process h_counter;
	
	h_sync: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			hsynch_i <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if pixel_counter = (H_ACTIVE + H_FRONT_PORCH - 1) then							--start of h_sync pulse
				hsynch_i <= '1';
			elsif pixel_counter = (H_TOTAL - H_BACK_PORCH - 1) then							--end of h_sync pulse
				hsynch_i <= '0';
			end if;
		end if;
	end process h_sync;
	
	v_counter: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			line_counter <= (others => '0');
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if line_counter = (V_TOTAL - 1) and pixel_counter = (H_TOTAL - 1) then		--last pixel of last line
				line_counter <= (others => '0');
			elsif pixel_counter = (H_TOTAL - 1) then												--last pixel of the current line
				line_counter <= line_counter + 1;
			end if;
		end if;
	end process v_counter;
	
	v_sync: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			vsynch_i <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if line_counter = (V_ACTIVE + V_FRONT_PORCH - 1) 
				and pixel_counter = (H_TOTAL - 1) then												--start of v_sync pulse
				vsynch_i <= '1';
			elsif line_counter = (V_TOTAL - V_BACK_PORCH - 1)
				and pixel_counter = (H_TOTAL - 1) then												--end of v_sync pulse
				vsynch_i <= '0';
			end if;
		end if;
	end process v_sync;
	
	h_blanking: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			h_blank <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if pixel_counter = (H_ACTIVE - 2) then													--start of h_blank (extra delay for composite blank)
				h_blank <= '1';
			elsif pixel_counter = (H_TOTAL - 2) then												--end of h_blank
				h_blank <= '0';
			end if;
		end if;
	end process h_blanking;
	
	v_blanking: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			v_blank <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if line_counter = (V_ACTIVE - 1)
				and pixel_counter = (H_TOTAL - 2) then												--start of v_blank (extra delay for composite blank)
				v_blank <= '1';
			elsif line_counter = (V_TOTAL - 1)
				and pixel_counter = (H_TOTAL - 2) then												--end of v_blank
				v_blank <= '0';
			end if;
		end if;
	end process v_blanking;
	
	comp_blanking: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			blank_i <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if v_blank = '1' or h_blank = '1' then
				blank_i <= '1';
			else
				blank_i <= '0';
			end if;
		end if;
	end process comp_blanking;
	
	h_sync_c: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			h_synch <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if pixel_counter = (H_ACTIVE + H_FRONT_PORCH - 2) then							--start of h_sync pulse
				h_synch <= '1';
			elsif pixel_counter = (H_TOTAL - H_BACK_PORCH - 2) then							--end of h_sync pulse
				h_synch <= '0';
			end if;
		end if;
	end process h_sync_c;
	
	v_sync_c: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			v_synch <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if line_counter = (V_ACTIVE + V_FRONT_PORCH - 1) 
				and pixel_counter = (H_TOTAL - 2) then												--start of v_sync pulse
				v_synch <= '1';
			elsif line_counter = (V_TOTAL - V_BACK_PORCH - 1)
				and pixel_counter = (H_TOTAL - 2) then												--start of v_sync pulse
				v_synch <= '0';
			end if;
		end if;
	end process v_sync_c;
	
	comp_synch: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			comp_synch_i <= '0';
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			comp_synch_i <= v_synch xor h_synch;
		end if;
	end process comp_synch;

	save_key_data: process (clk_40M, reset)
	begin
		if reset = '0' then
			key_data <= (others => '0');
		elsif clk_40M'event and clk_40M = '1' then
			if keyboard_data_valid = '1' then
				key_data <= keyboard_control_data;
			else
				key_data <= key_data;
			end if;
		end if;
	end process;
	
	read_character: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			rom_address 	<= (others => '0');
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if (char_h_position = 0 and char_v_position = 0) then	--first row + first column
				rom_address <= (ADR_P + char_v_count);
			elsif (char_h_position = 1 and char_v_position = 0) then	--first row + second column
				rom_address <= (ADR_R + char_v_count);
			elsif (char_h_position = 2 and char_v_position = 0) then
				rom_address <= (ADR_A + char_v_count);
			elsif (char_h_position = 3 and char_v_position = 0) then
				rom_address <= (ADR_G + char_v_count);
			elsif (char_h_position = 4 and char_v_position = 0) then
				rom_address <= (ADR_M + char_v_count);
			elsif (char_h_position = 5 and char_v_position = 0) then
				rom_address <= (ADR_A + char_v_count);
			elsif (char_h_position = 6 and char_v_position = 0) then
				rom_address <= (ADR_SPACE + char_v_count);
			elsif (char_h_position = 7 and char_v_position = 0) then
				rom_address <= (ADR_V + char_v_count);
			elsif (char_h_position = 8 and char_v_position = 0) then
				rom_address <= (ADR_1 + char_v_count);
			elsif (char_h_position = 9 and char_v_position = 0) then
				rom_address <= (ADR_DOT + char_v_count);
			elsif (char_h_position = 10 and char_v_position = 0) then
				rom_address <= (ADR_0 + char_v_count);
			elsif (char_h_position = 11 and char_v_position = 0) then
				rom_address <= (ADR_0 + char_v_count);
			
			elsif (char_h_position = 0 and char_v_position = 1) then --second row + first column
				rom_address <= (ADR_B + char_v_count);
			elsif (char_h_position = 1 and char_v_position = 1) then
				rom_address <= (ADR_Y + char_v_count);
			elsif (char_h_position = 2 and char_v_position = 1) then
				rom_address <= (ADR_COLON + char_v_count);
			elsif (char_h_position = 4 and char_v_position = 1) then
				rom_address <= (ADR_S + char_v_count);
			elsif (char_h_position = 5 and char_v_position = 1) then
				rom_address <= (ADR_I + char_v_count);
			elsif (char_h_position = 6 and char_v_position = 1) then
				rom_address <= (ADR_E + char_v_count);
			elsif (char_h_position = 7 and char_v_position = 1) then
				rom_address <= (ADR_B + char_v_count);
			elsif (char_h_position = 8 and char_v_position = 1) then
				rom_address <= (ADR_E + char_v_count);
			elsif (char_h_position = 10 and char_v_position = 1) then
				rom_address <= (ADR_K + char_v_count);
			elsif (char_h_position = 11 and char_v_position = 1) then
				rom_address <= (ADR_R + char_v_count);
			elsif (char_h_position = 12 and char_v_position = 1) then
				rom_address <= (ADR_I + char_v_count);
			elsif (char_h_position = 13 and char_v_position = 1) then
				rom_address <= (ADR_J + char_v_count);
			elsif (char_h_position = 14 and char_v_position = 1) then
				rom_address <= (ADR_G + char_v_count);
			elsif (char_h_position = 15 and char_v_position = 1) then
				rom_address <= (ADR_S + char_v_count);
			elsif (char_h_position = 16 and char_v_position = 1) then
				rom_address <= (ADR_M + char_v_count);
			elsif (char_h_position = 17 and char_v_position = 1) then
				rom_address <= (ADR_A + char_v_count);
			elsif (char_h_position = 18 and char_v_position = 1) then
				rom_address <= (ADR_N + char_v_count);
			
			--show key pressed
			--elsif (char_h_position = 0 and char_v_position = 2) then
			--	rom_address <= (key_data + char_v_count);
			
			elsif (char_h_position = 0 and char_v_position = 4) then
				rom_address <= (ADR_C + char_v_count);
			elsif (char_h_position = 1 and char_v_position = 4) then
				rom_address <= (ADR_O + char_v_count);
			elsif (char_h_position = 2 and char_v_position = 4) then
				rom_address <= (ADR_N + char_v_count);
			elsif (char_h_position = 3 and char_v_position = 4) then
				rom_address <= (ADR_T + char_v_count);
			elsif (char_h_position = 4 and char_v_position = 4) then
				rom_address <= (ADR_R + char_v_count);
			elsif (char_h_position = 5 and char_v_position = 4) then
				rom_address <= (ADR_O + char_v_count);
			elsif (char_h_position = 6 and char_v_position = 4) then
				rom_address <= (ADR_L + char_v_count);
			elsif (char_h_position = 8 and char_v_position = 4) then
				rom_address <= (ADR_S + char_v_count);
			elsif (char_h_position = 9 and char_v_position = 4) then
				rom_address <= (ADR_I + char_v_count);
			elsif (char_h_position = 10 and char_v_position = 4) then
				rom_address <= (ADR_G + char_v_count);
			elsif (char_h_position = 11 and char_v_position = 4) then
				rom_address <= (ADR_N + char_v_count);
			elsif (char_h_position = 12 and char_v_position = 4) then
				rom_address <= (ADR_A + char_v_count);
			elsif (char_h_position = 13 and char_v_position = 4) then
				rom_address <= (ADR_L + char_v_count);
			elsif (char_h_position = 14 and char_v_position = 4) then
				rom_address <= (ADR_S + char_v_count);
			elsif (char_h_position = 15 and char_v_position = 4) then
				rom_address <= (ADR_COLON + char_v_count);
			
			elsif (char_h_position = 0 and char_v_position = 5) then
				rom_address <= (ADR_M + char_v_count);
			elsif (char_h_position = 1 and char_v_position = 5) then
				rom_address <= (ADR_A + char_v_count);
			elsif (char_h_position = 2 and char_v_position = 5) then
				rom_address <= (ADR_I + char_v_count);
			elsif (char_h_position = 3 and char_v_position = 5) then
				rom_address <= (ADR_N + char_v_count);
			elsif (char_h_position = 4 and char_v_position = 5) then
				rom_address <= (ADR_COLON + char_v_count);
			elsif (char_h_position = 6 and char_v_position = 5) then
				rom_address <= (ADR_1 + char_v_count);
			elsif (char_h_position = 7 and char_v_position = 5) then
				rom_address <= (ADR_TILDE + char_v_count);
			elsif (char_h_position = 8 and char_v_position = 5) then
				rom_address <= (ADR_8 + char_v_count);
				
			elsif (char_h_position = 0 and char_v_position = 6) then
				rom_address <= (ADR_T + char_v_count);
			elsif (char_h_position = 1 and char_v_position = 6) then
				rom_address <= (ADR_I + char_v_count);
			elsif (char_h_position = 2 and char_v_position = 6) then
				rom_address <= (ADR_M + char_v_count);
			elsif (char_h_position = 3 and char_v_position = 6) then
				rom_address <= (ADR_E + char_v_count);
			elsif (char_h_position = 4 and char_v_position = 6) then
				rom_address <= (ADR_COLON + char_v_count);
			elsif (char_h_position = 6 and char_v_position = 6) then
				rom_address <= (ADR_Q + char_v_count);
			elsif (char_h_position = 7 and char_v_position = 6) then
				rom_address <= (ADR_TILDE + char_v_count);
			elsif (char_h_position = 8 and char_v_position = 6) then
				rom_address <= (ADR_I + char_v_count);
				
			elsif (char_h_position = 0 and char_v_position = 7) then
				rom_address <= (ADR_F + char_v_count);
			elsif (char_h_position = 1 and char_v_position = 7) then
				rom_address <= (ADR_R + char_v_count);
			elsif (char_h_position = 2 and char_v_position = 7) then
				rom_address <= (ADR_E + char_v_count);
			elsif (char_h_position = 3 and char_v_position = 7) then
				rom_address <= (ADR_Q + char_v_count);
			elsif (char_h_position = 4 and char_v_position = 7) then
				rom_address <= (ADR_COLON + char_v_count);
			elsif (char_h_position = 6 and char_v_position = 7) then
				rom_address <= (ADR_A + char_v_count);
			elsif (char_h_position = 7 and char_v_position = 7) then
				rom_address <= (ADR_TILDE + char_v_count);
			elsif (char_h_position = 8 and char_v_position = 7) then
				rom_address <= (ADR_K + char_v_count);
			
			else
				rom_address <= (others => '0');					--empty char
			end if;
		end if;
	end process read_character;
	
	calc_char_count: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			char_v_count 		<= (others => '0');
			char_h_count 		<= 0;
			char_v_position 	<= 0;
			char_h_position 	<= 0;
			char_matrix			<= (others => '0');
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			if line_counter >= V_FRONT_PORCH and line_counter < (V_ACTIVE_D2 + V_FRONT_PORCH) and
				pixel_counter >= H_ACTIVE_D2 and pixel_counter < (H_ACTIVE + H_FRONT_PORCH) then
				if char_h_count = 10 then
					char_h_count <= 0;
					char_h_position <= char_h_position + 1;
				elsif char_h_count = 1 then			--data request from rom should be ready now
					char_matrix <= rom_data;
				else
					char_h_count <= char_h_count + 1;
				end if;
			elsif line_counter > V_FRONT_PORCH and line_counter < (V_ACTIVE_D2 + V_FRONT_PORCH) then
				if char_v_count = 13 then
					char_v_count <= (others => '0');
					char_v_position <= char_v_position + 1;
				elsif pixel_counter = 0 then			--start of new line
					char_v_count <= char_v_count + 1;
					char_h_count <= 0;
					char_h_position <= 0;
				end if;
			else
				char_h_count <= 0;
				char_v_count <= (others => '0');
				char_h_position <= 0;
				char_v_position <= 0;
			end if;
		end if;
	end process calc_char_count;
	
	--sample the fft data 0 (before)
	sample_fft_data_0: process (clk_48k, reset)
	begin
		if reset = '0' then
			graph_data_0 <= (others => (others => '0'));
			temp_data_0 <= (others => (others => '0'));
			end_of_video_frame_0 <= '1';
			start_new_audio_frame_0 <= '0';
			audio_frame_ready_0 <= '0';
			begin_fetch_0 <= '0';
		elsif clk_48k'event and clk_48k = '1' then
			if end_of_video_frame_0 = '1' then								--update data at end of frame
				graph_data_0					<= temp_data_0;
				end_of_video_frame_0		<= '0';
				start_new_audio_frame_0	<= '1';
				begin_fetch_0					<= '0';
				temp_data_0					<= (others => (others => '0'));
				last_count_0					<= (others => '0');
			elsif sample_number = 0 and start_new_audio_frame_0 = '1' then		--new sample ready
				begin_fetch_0 				<= '1';
				start_new_audio_frame_0 	<= '0';
				temp_data_0(0) 				<= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
			--we don't use sample 64-127 as they are the mirror frequencies of the first 64 samples
			--the last part of this spectrum (~18kHz - 24kHz) is usually considered out of audible range
			--however, it is displayed as a formality. Also because of "smearing" there will still be 
			--excited frequencies in these bands.
			elsif begin_fetch_0 = '1' and not(sample_number = last_count_0) then
				if sample_number = 63 then						--sample 63			(24000 Hz)				--LAST SAMPLE
					temp_data_0(7) 				<= shr(temp_data_0(7), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
					start_new_audio_frame_0 	<= '0';
					end_of_video_frame_0		<= '1';
					begin_fetch_0				<= '0';
				elsif sample_number > 56 then					--sample 57 - 62	(21375 - 13625 Hz)	--HIGH FREQUENCIES
					temp_data_0(7) <= shr(temp_data_0(7), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 56 then					--sample 56			(21000 Hz)
					temp_data_0(7) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number > 48 then					--sample 49 - 55	(18375 - 20625 Hz)
					temp_data_0(6) <= shr(temp_data_0(6), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 48 then					--sample 48			(18000 Hz)										--END AUDIBLE RANGE
					temp_data_0(6) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number > 40 then					--sample 41 - 47	(15375 - 17625 Hz)
					temp_data_0(5) <= shr(temp_data_0(5), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 40 then					--sample 40			(15000 Hz)
					temp_data_0(5) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number > 32 then					--sample 33 - 39	(12375 - 14625 Hz)
					temp_data_0(4) <= shr(temp_data_0(4), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 32 then					--sample 32			(12000 Hz)
					temp_data_0(4) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number > 24 then					--sample 25 - 31	(9375 - 11625 Hz)
					temp_data_0(3) <= shr(temp_data_0(3), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 24 then					--sample 24			(9000 Hz)
					temp_data_0(3) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number > 16 then					--sample 17 - 23	(6375 - 8625 Hz)
					temp_data_0(2) <= shr(temp_data_0(2), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 16 then					--sample 16			(6000 Hz)
					temp_data_0(2) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number > 8 then					--sample 9 - 15	(3375 - 5625 Hz)
					temp_data_0(1) <= shr(temp_data_0(1), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				elsif sample_number = 8 then					--sample 8			(3000 Hz)
					temp_data_0(1) <= get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0);
				elsif sample_number >= 1 then					--sample 1 - 7		(375 - 2625 Hz)	--LOW FREQUENCIES		--START AUDIBLE RANGE
					temp_data_0(0) <= shr(temp_data_0(0), "1") + shr(get_dist(fft_data_re_in_left_0, fft_data_im_in_left_0),"1");
				end if;
				last_count_0 <= sample_number;
			end if;
		end if;
	end process;
	
	--sample the fft data 1 (after)
	sample_fft_data_1: process (clk_48k, reset)
	begin
		if reset = '0' then
			graph_data_1 <= (others => (others => '0'));
			temp_data_1 <= (others => (others => '0'));
			end_of_video_frame_1 <= '1';
			start_new_audio_frame_1 <= '0';
			audio_frame_ready_1 <= '0';
			begin_fetch_1 <= '0';
		elsif clk_48k'event and clk_48k = '1' then
			if end_of_video_frame_1 = '1' then								--update data at end of frame
				graph_data_1					<= temp_data_1;
				end_of_video_frame_1		<= '0';
				start_new_audio_frame_1	<= '1';
				begin_fetch_1					<= '0';
				temp_data_1					<= (others => (others => '0'));
				last_count_1					<= (others => '0');
			elsif sample_number = 0 and start_new_audio_frame_1 = '1' then		--new sample ready
				begin_fetch_1 				<= '1';
				start_new_audio_frame_1 	<= '0';
				temp_data_1(0) 				<= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
			--we don't use sample 64-127 as they are the mirror frequencies of the first 64 samples
			--the last part of this spectrum (~18kHz - 24kHz) is usually considered out of audible range
			--however, it is displayed as a formality. Also because of "smearing" there will still be 
			--excited frequencies in these bands.
			elsif begin_fetch_1 = '1' and not(sample_number = last_count_1) then
				if sample_number = 63 then						--sample 63			(24000 Hz)				--LAST SAMPLE
					temp_data_1(7) 				<= shr(temp_data_1(7), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
					start_new_audio_frame_1 	<= '0';
					end_of_video_frame_1		<= '1';
					begin_fetch_1				<= '0';
				elsif sample_number > 56 then					--sample 57 - 62	(21375 - 13625 Hz)	--HIGH FREQUENCIES
					temp_data_1(7) <= shr(temp_data_1(7), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 56 then					--sample 56			(21000 Hz)
					temp_data_1(7) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number > 48 then					--sample 49 - 55	(18375 - 20625 Hz)
					temp_data_1(6) <= shr(temp_data_1(6), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 48 then					--sample 48			(18000 Hz)										--END AUDIBLE RANGE
					temp_data_1(6) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number > 40 then					--sample 41 - 47	(15375 - 17625 Hz)
					temp_data_1(5) <= shr(temp_data_1(5), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 40 then					--sample 40			(15000 Hz)
					temp_data_1(5) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number > 32 then					--sample 33 - 39	(12375 - 14625 Hz)
					temp_data_1(4) <= shr(temp_data_1(4), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 32 then					--sample 32			(12000 Hz)
					temp_data_1(4) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number > 24 then					--sample 25 - 31	(9375 - 11625 Hz)
					temp_data_1(3) <= shr(temp_data_1(3), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 24 then					--sample 24			(9000 Hz)
					temp_data_1(3) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number > 16 then					--sample 17 - 23	(6375 - 8625 Hz)
					temp_data_1(2) <= shr(temp_data_1(2), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 16 then					--sample 16			(6000 Hz)
					temp_data_1(2) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number > 8 then					--sample 9 - 15	(3375 - 5625 Hz)
					temp_data_1(1) <= shr(temp_data_1(1), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				elsif sample_number = 8 then					--sample 8			(3000 Hz)
					temp_data_1(1) <= get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1);
				elsif sample_number >= 1 then					--sample 1 - 7		(375 - 2625 Hz)	--LOW FREQUENCIES		--START AUDIBLE RANGE
					temp_data_1(0) <= shr(temp_data_1(0), "1") + shr(get_dist(fft_data_re_in_left_1, fft_data_im_in_left_1),"1");
				end if;
				last_count_1 <= sample_number;
			end if;
		end if;
	end process;
	
	--create readable output without the use of characters
	logo: process (pixel_clk_i, reset)
	begin
		if reset = '0' then
			VGA_OUT_RED 	<= (others => '0');
			VGA_OUT_GREEN 	<= (others => '0');
			VGA_OUT_BLUE	<= (others => '0');
		elsif pixel_clk_i'event and pixel_clk_i = '1' then
			--control data
			--time control out
			if line_counter >= 5 and line_counter < 10 and
				pixel_counter >= 50 and pixel_counter < 60 then
				if time_control_out(3) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 5 and line_counter < 10 and
				pixel_counter >= 60 and pixel_counter < 70 then
				if time_control_out(2) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 5 and line_counter < 10 and
				pixel_counter >= 70 and pixel_counter < 80 then
				if time_control_out(1) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 5 and line_counter < 10 and
				pixel_counter >= 80 and pixel_counter < 90 then
				if time_control_out(0) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			
			--fft control out
			elsif line_counter >= 15 and line_counter < 20 and
				pixel_counter >= 50 and pixel_counter < 60 then
				if freq_control_out(3) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 15 and line_counter < 20 and
				pixel_counter >= 60 and pixel_counter < 70 then
				if freq_control_out(2) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 15 and line_counter < 20 and
				pixel_counter >= 70 and pixel_counter < 80 then
				if freq_control_out(1) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 15 and line_counter < 20 and
				pixel_counter >= 80 and pixel_counter < 90 then
				if freq_control_out(0) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			
			--key data
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 50 and pixel_counter < 60 then
				if key_data(7) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 60 and pixel_counter < 70 then
				if key_data(6) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 70 and pixel_counter < 80 then
				if key_data(5) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 80 and pixel_counter < 90 then
				if key_data(4) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 90 and pixel_counter < 100 then
				if key_data(3) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 100 and pixel_counter < 110 then
				if key_data(2) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 110 and pixel_counter < 120 then
				if key_data(1) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 25 and line_counter < 30 and
				pixel_counter >= 120 and pixel_counter < 130 then
				if key_data(0) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			
			--main control in data
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 50 and pixel_counter < 60 then
				if main_control_in(7) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 60 and pixel_counter < 70 then
				if main_control_in(6) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 70 and pixel_counter < 80 then
				if main_control_in(5) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 80 and pixel_counter < 90 then
				if main_control_in(4) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 90 and pixel_counter < 100 then
				if main_control_in(3) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 100 and pixel_counter < 110 then
				if main_control_in(2) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 110 and pixel_counter < 120 then
				if main_control_in(1) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 45 and line_counter < 50 and
				pixel_counter >= 120 and pixel_counter < 130 then
				if main_control_in(0) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
				
			--time control in data
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 50 and pixel_counter < 60 then
				if time_control_in(7) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 60 and pixel_counter < 70 then
				if time_control_in(6) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 70 and pixel_counter < 80 then
				if time_control_in(5) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 80 and pixel_counter < 90 then
				if time_control_in(4) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 90 and pixel_counter < 100 then
				if time_control_in(3) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 100 and pixel_counter < 110 then
				if time_control_in(2) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 110 and pixel_counter < 120 then
				if time_control_in(1) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 55 and line_counter < 60 and
				pixel_counter >= 120 and pixel_counter < 130 then
				if time_control_in(0) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
				
			--freq control in data
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 50 and pixel_counter < 60 then
				if freq_control_in(7) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 60 and pixel_counter < 70 then
				if freq_control_in(6) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 70 and pixel_counter < 80 then
				if freq_control_in(5) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 80 and pixel_counter < 90 then
				if freq_control_in(4) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 90 and pixel_counter < 100 then
				if freq_control_in(3) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 100 and pixel_counter < 110 then
				if freq_control_in(2) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 110 and pixel_counter < 120 then
				if freq_control_in(1) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			elsif line_counter >= 65 and line_counter < 70 and
				pixel_counter >= 120 and pixel_counter < 130 then
				if freq_control_in(0) = '1' then
					VGA_OUT_RED 	<= X"00";
					VGA_OUT_GREEN 	<= X"FF";
					VGA_OUT_BLUE	<= X"00";
				else
					VGA_OUT_RED 	<= X"FF";
					VGA_OUT_GREEN 	<= X"00";
					VGA_OUT_BLUE	<= X"00";
				end if;
			
			--CHARACTER MAPPED SCREEN PART (TOP RIGHT)
			elsif line_counter >= V_FRONT_PORCH and line_counter < (V_ACTIVE_D2 + V_FRONT_PORCH) and
				pixel_counter >= H_ACTIVE_D2 and pixel_counter < (H_ACTIVE + H_FRONT_PORCH) then
				if not(char_v_count = 0 or char_v_count = 13 or char_h_count = 0 or char_h_count = 10) then --spacing
					if char_matrix(10-char_h_count) = '1' then
						VGA_OUT_RED 	<= X"FF";
						VGA_OUT_GREEN 	<= X"00";
						VGA_OUT_BLUE	<= X"00";
					else
						VGA_OUT_RED 	<= X"0F";
						VGA_OUT_GREEN 	<= X"3F";
						VGA_OUT_BLUE	<= X"3F";
					end if;
				else
						VGA_OUT_RED 	<= X"0F";
						VGA_OUT_GREEN 	<= X"3F";
						VGA_OUT_BLUE	<= X"3F";
				end if;
				
			--axes
			elsif line_counter >= 450 and line_counter < 578 and
				pixel_counter >= 30 and pixel_counter < 32 then
				VGA_OUT_RED 	<= X"FF";
				VGA_OUT_GREEN 	<= X"FF";
				VGA_OUT_BLUE	<= X"FF";
			elsif line_counter >= 578 and line_counter < 579 and
				pixel_counter >= 30 and pixel_counter < 200 then
				VGA_OUT_RED 	<= X"FF";
				VGA_OUT_GREEN 	<= X"FF";
				VGA_OUT_BLUE	<= X"FF";
			
			--bars
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(0)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 33 and pixel_counter < 52 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(1)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 54 and pixel_counter < 73 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(2)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 75 and pixel_counter < 94 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(3)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 96 and pixel_counter < 115 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(4)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 117 and pixel_counter < 136 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(5)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 138 and pixel_counter < 157 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(6)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 159 and pixel_counter < 178 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0111000010" + ("010000000" - graph_data_1(7)(15 downto 9)) and line_counter < 576 and
				pixel_counter >= 180 and pixel_counter < 199 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			
			--axes
			elsif line_counter >= 300 and line_counter < 428 and
				pixel_counter >= 30 and pixel_counter < 32 then
				VGA_OUT_RED 	<= X"FF";
				VGA_OUT_GREEN 	<= X"FF";
				VGA_OUT_BLUE	<= X"FF";
			elsif line_counter >= 428 and line_counter < 429 and
				pixel_counter >= 30 and pixel_counter < 200 then
				VGA_OUT_RED 	<= X"FF";
				VGA_OUT_GREEN 	<= X"FF";
				VGA_OUT_BLUE	<= X"FF";
			
			--bars
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(0)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 33 and pixel_counter < 52 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(1)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 54 and pixel_counter < 73 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(2)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 75 and pixel_counter < 94 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(3)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 96 and pixel_counter < 115 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(4)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 117 and pixel_counter < 136 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(5)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 138 and pixel_counter < 157 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(6)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 159 and pixel_counter < 178 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			elsif line_counter >= "0100101100" + ("010000000" - graph_data_0(7)(15 downto 9)) and line_counter < 426 and
				pixel_counter >= 180 and pixel_counter < 199 then
				VGA_OUT_RED		<= X"0F";
				VGA_OUT_GREEN 	<= X"5F";
				VGA_OUT_BLUE	<= X"7F";
			
			else
				VGA_OUT_RED 	<= X"0F";
				VGA_OUT_GREEN 	<= X"0F";
				VGA_OUT_BLUE	<= X"0F";
			end if;
		end if;
	end process;
end Behavioral;

